//
//  POSApplyListVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/5/6.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "POSApplyListVC.h"
#import "POSApplyListCell.h"
#import "POSApplyListModel.h"
#import "POSApplyListStore.h"

@interface POSApplyListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger totalPage;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation POSApplyListVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    [self setNavBarWithTitle:@"申请记录"];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"POSApplyListCell" bundle:nil] forCellReuseIdentifier:@"POSApplyListCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.sectionHeaderHeight = 0.01f;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        if (self.page < self.totalPage) {
            self.page ++ ;
            [self loadData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.page = 1;
    self.totalPage = 1;
}
- (void)loadData{
    [[[POSApplyListStore alloc]init] GetApplyListWithPrams:@{@"size":@"100",@"current":[NSString stringWithFormat:@"%ld",self.page]} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.totalPage = [[NSString stringWithFormat:@"%@",res[@"data"][@"pages"]] integerValue];
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                for (NSDictionary *dic in arr) {
                    POSApplyListModel *model = [POSApplyListModel mj_objectWithKeyValues:dic];
                    [self.dataArr addObject:model];
                }
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    footer.backgroundColor = [UIColor clearColor];
    return footer;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    POSApplyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"POSApplyListCellID"];
    if (self.dataArr.count > 0) {
        cell.model = self.dataArr[indexPath.section];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
